/**
 * 判断数据类型
 */
function type (obj) {
  if (!obj) return
  const o = {
    '[object Array]': 'array',
    '[object Boolean]': 'boolean',
    '[object Date]': 'date',
    '[object Function]': 'function',
    '[object Number]': 'number',
    '[object Object]': 'object',
    '[object RegExp]': 'regexp',
    '[object String]': 'string'
  }
  let ts = Object.prototype.toString.call(obj)
  return o[ts]
}
/**
 * 判断是否为纯对象(朴素对象)
 */
function isPlainObject (obj) {
  return 'isPrototypeOf' in obj && type(obj) === 'object'
}
const isArray = Array.isArray || function (obj) {
  return type(obj) === 'array'
}
/**
 * 深拷贝，引入lodash之后已无用
 */
export const extend = (...args) => {
  let options
  let name
  let src
  let copy
  let copyIsArray
  let clone
  let target = args[0] || {}
  let i = 1
  let length = args.length
  let deep = false
  if (typeof target === 'boolean') {
    deep = target
    target = args[1] || {}
    i = 2
  }
  if (type(target) !== 'object' && !type(target) !== 'function') {
    target = {}
  }
  if (length === i) {
    target = this
    --i
  }
  for (; i < length; i++) {
    if ((options = args[i]) != null) {
      for (name in options) {
        src = target[name]
        copy = options[name]
        if (target === copy) {
          continue
        }
        if (deep && copy && (isPlainObject(copy) || (copyIsArray = isArray(copy)))) {
          if (copyIsArray) {
            copyIsArray = false
            clone = src && isArray(src) ? src : []
          } else {
            clone = src && isPlainObject(src) ? src : {}
          }
          target[name] = extend(deep, clone, copy)
        } else if (copy !== undefined) {
          target[name] = copy
        }
      }
    }
  }
  return target
}
